package com.ruibang.glass.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.portal.constant.Constants;
import com.ruibang.glass.portal.entity.UserEntity;
import com.ruibang.glass.portal.service.ProductService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teaming.cloud.framework2.common.model.ResultBody;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.ruibang.glass.portal.entity.Product;
import lombok.Data;

import java.util.List;


/**
 * <p>
 * 产品管理信息 前端控制器
 * </p>
 *
 * @author wujie
 * @since 2024-03-28
 */
@RestController
@RequestMapping("/api/sys/product")
@Api(value = "产品管理信息控制器", tags = {"产品管理信息接口"})
public class ProductController {


    @Autowired
    private ProductService productService;

    @Autowired
    private RedisSingleService redisService;


    /**
     * 通过id查询
     */
    @GetMapping("/getById/{id}")
    @ApiOperation(value = "获取产品管理信息", notes = "获取产品管理信息")
    public ResultBody getById(@PathVariable(value = "id") String id) {
        return ResultBody.success(productService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增产品管理信息", notes = "新增产品管理信息")
    public ResultBody save(@RequestBody Product product) {
        productService.save(product);
        redisService.hset(RedisKey.PRODUCT_MANAGEMENT_GROUP_TYPE,product.getProductCode(),product.getProductName());
        return ResultBody.success();
    }

    /**
     * 通过id删除
     */
    @GetMapping("/deleteById/{id}")
    @ApiOperation(value = "通过ID删除产品管理信息", notes = "通过ID删除产品管理信息")
    public ResultBody delete(@PathVariable(value = "id") String id) {
        Product product = productService.getById(id);
        productService.delete(id);
        redisService.hdel(RedisKey.PRODUCT_MANAGEMENT_GROUP_TYPE,product.getProductCode());
        return ResultBody.success();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改产品管理信息", notes = "修改产品管理信息")
    public ResultBody updateById(@RequestBody Product product) {
        productService.updateById(product);
        redisService.hset(RedisKey.PRODUCT_MANAGEMENT_GROUP_TYPE,product.getProductCode(),product.getProductName());
        return ResultBody.success();
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页列表查询产品管理信息", notes = "分页列表查询产品管理信息")
    public ResultBody page(@RequestBody PageRequest<Product> pageParam) {
        PageResult<Product> pr = productService.queryPage(pageParam);
        return ResultBody.success(pr);
    }

    @GetMapping("/getProductSelectList")
    @ApiOperation(value = "获取产品的下拉集合", notes = "获取产品的下拉集合")
    public ResultBody getProductSelectList() {
        List<Product> list = productService.list(Wrappers.<Product>lambdaQuery().
                select(Product::getProductName, Product::getProductCode, Product::getProductId, Product::getStandard, Product::getUnit)
                .eq(Product::getIsDelete, Constants.ZERO_STR)
        );
        return ResultBody.success(list);
    }

    @GetMapping("/getStandardSelectList")
    @ApiOperation(value = "执行标准下拉", notes = "执行标准下拉")
    public ResultBody getStandardSelectList() {
        List<Product> list = productService.list(Wrappers.<Product>lambdaQuery().
                select(Product::getProductId, Product::getStandard)
                .eq(Product::getIsDelete, Constants.ZERO_STR)
        );
        return ResultBody.success(list);
    }



}
